///
/// ArmyBodger 3
/// (c)2010 richyp
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;

namespace ArmyBodger.Core {
    /// <summary>
    /// Parser to load an ArmyBodger save file
    /// </summary>
    public class SaveFileParser {
        /// <summary>
        /// Load the specified file, using the armybook and gamesystem passed to this
        /// method
        /// </summary>
        /// <param name="armyBook">The ArmyBook this save file uses</param>
        /// <param name="doc">The XML save file</param>
        /// <param name="gameSystem">The GameSystem the ArmyBook uses</param>
        /// <returns>A Collection of Entries retrieved from the Save File</returns>
        public EntryCollection Load(ArmyBook armyBook, XDocument doc, GameSystem gameSystem) {
            EntryCollection entries = new EntryCollection();
            foreach (XElement entryElement in doc.XPathSelectElements("list/entry")) {
                string name = entryElement.Attribute("name").Value;
                string category = entryElement.Attribute("category").Value;
                Entry entry = armyBook.AvailableEntries[name].CreateDefaultEntry();
                entry.Units.Clear();
                foreach (XElement upgradeElement in entryElement.XPathSelectElements("upgrade")) {
                    string upgradeName = upgradeElement.Attribute("name").Value;
                    foreach (Upgrade upgrade in entry.Upgrades) {
                        if (upgrade.Equipment.Name == upgradeName) {
                            upgrade.Applied = true;
                        }
                    }
                }
                foreach (XElement tagElement in entryElement.XPathSelectElements("tags/tag")) {
                    entry.SetTag(
                        tagElement.Attribute("name").Value,
                        tagElement.Value
                        );
                }
                foreach (XElement unitElement in entryElement.XPathSelectElements("units/unit")) {
                    foreach (Unit unit in entry.PossibleUnits) {
                        if (unit.Name == unitElement.Attribute("name").Value) {
                            Unit newUnit = unit.Copy(entry);
                            foreach (XElement optionElement in unitElement.XPathSelectElements("option")) {
                                string optionName = optionElement.Attribute("name").Value;
                                foreach (EquipmentOption option in newUnit.Equipment) {
                                    if (option.Equipment.Name == optionName) {
                                        option.Equipped = true;
                                    }
                                    if (optionElement.Attribute("replacement") != null) {
                                        option.EquipReplacement(optionElement.Attribute("replacement").Value);
                                    }
                                }
                            }
                            entry.Units.Add(newUnit);
                        }
                    }
                }
                entries.Add(entry);
            }
            return entries;
        }
    }
}
